<?php

declare(strict_types=1);

namespace app\admin\service;

use \app\admin\model\MarketSmCopywriting as MarketSmCopywritingModel;
use app\admin\service\CryptoNews as CryptoNewsService;
use app\api\model\ProductBrand;
use app\common\Common;
use app\common\RedisClient;

class Crontab
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * 定时任务生成文案
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-11-05 11 29 04
     * author: jiang
     */
    public function generationSMCopywriting($limit)
    {
        $oMarketSmCopywritingModel = MarketSmCopywritingModel::with('task')->where([
            ['task_status', '=', 0],
            ['redo_num', '<', 5]
            //            ['sm_generate_opywriting_tasks_id','=','not null']
        ])->order('id asc')->limit($limit)->select();

        foreach ($oMarketSmCopywritingModel as $value) {
     
            $param = [
                'id' => $value->task ? $value->task->crypto_news_id : '',
                'platform' => $value->task ? $value->task->channel_id : '',
                'cue_word' => $value->task ? $value->task->cue_word : '',
                'language' => $value->task ? $value->task->language_id : '',
                'associatedMiners' => $value->task ? $value->task->mining_machine : '{}',
                'writing_id' => $value->id
            ];
            $oCryptoNewsService = new CryptoNewsService();
 
            $res = $oCryptoNewsService->publish($param, false);
            //            if ($res['code'] != 0) {
            //                return ['code' => 1, 'message' => $res['message']];
            //            }
        }
        return ['code' => 0, 'message' => '操作成功！'];
    }

    public function getProfitForCache()
    {
        set_time_limit(3000);
        ini_set('max_execution_time', 3000);
    // 设置内存限制为 512MB
        ini_set('memory_limit', '1G');
        $func = 'getProductList';
        $redis = New RedisClient();
        #批量获取商品信息
        $product = new \app\api\model\Product();
        $productService = new \app\api\service\Product();
        $produtList = $product->getProductInfoForProfit(0,$func);
        $total = [];
        if($produtList){
            #翻译支持语言
            $common = new \app\api\service\Common();
            $language = $common::LANGUAGE_CODE;
            $profitData = $productService->getProfitInfoForCache($produtList);
            $coins = [
            ];
            $algorithms = [
            ];
            $brandIds = array_unique(array_column($profitData,'brand'));
            $brandInfo = ProductBrand::whereIn('id',$brandIds)->field('id as lable,name,image')->select()->toArray();
            if($brandInfo){
                $redis->set('brandList',json_encode($brandInfo));
            }
            $returnAlg = array_unique(array_column($profitData,'algorithm'));
            foreach ($returnAlg as $item){
                $algorithms[] = ['name'=>$item,'lable'=>$item];
            }
            $coins_base = [];
            foreach ($profitData as $profitDatum){
                if($profitDatum['coins_data']){
                    foreach ($profitDatum['coins_data'] as $coins_datum){
                        if(!in_array($coins_datum['short_name'],$coins_base)){
                            $coins_base[] = $coins_datum['short_name'];
                        }
                    }
                }
            }
            foreach ($coins_base as $valueCoin){
                $coins[] = ['name'=>$valueCoin,'lable'=>$valueCoin,'image'=>$productService->getCoinsUrl($valueCoin)];
            }
            $redis->set('coinList',json_encode($coins));
            $redis->set('algorithmList',json_encode($algorithms));
            foreach ($language as $k=>$v){
                $data = $productService->formateForCache($profitData,$produtList,$k);
                if($data){
                    $redisKey = 'getProductList:'.$k;
                    $redis->set($redisKey,json_encode($data)) ;
                }
            }

        }
        return true;
    }
}
